package parsing

import (
	"data-process/logger"
	"fmt"
	"strings"
)

type FileType string

const (
	String  FileType = "String"
	Number  FileType = "Number"
	Boolean FileType = "Boolean"
	Array   FileType = "Array"
	Null    FileType = "Null"
)

// AbsParsing 抽象解析
type AbsParsing interface {
	Build() (string, []interface{}, error) // Build 构建数据，通过单向channel返回
}

type Parsing struct {
	FieldName  string
	FieldValue string
	FieldTag   string
	Lists      chan interface{}
	Num        int
	key        string
	value      interface{}
}

// NewParsing 通过解析value的类型，决定分配搭配指定的解析器中，并对key做必要的简单解析
func NewParsing(key string, value interface{}, size int) *Parsing {
	return &Parsing{key: key, value: value, Num: size}
}

// ParseFactory 解析工厂
func (p *Parsing) ParseFactory() AbsParsing {
	var absParsing AbsParsing
	switch i := p.value.(type) {
	case string:
		absParsing = NewStringParse(p.key, i, p.Num)
		break
	case bool:
		absParsing = NewBoolParsing(p.key, i, p.Num)
		break
	case float64:
		tempValue := fmt.Sprintf("%v", i)
		if strings.Contains(tempValue, ".") {
			absParsing = NewFloatParse(p.key, i, p.Num)
		} else {
			absParsing = NewIntegerParse(p.key, int(i), p.Num)
		}
		break
	default:
		logger.Fatalf("当前的value：%v，可能是不支持类型\n", p.value)
	}
	return absParsing
}
